<template>
  <div class="register">
    <form>
      <div class="lable">
        <label class="error">{{ message }}</label>
        <input
          name="username"
          type="text"
          v-model.lazy="username"
          placeholder="请输入用户名"
        />
        <input
          type="password"
          v-model.trim="password"
          placeholder="请输入密码"
        />
        <input
          type="password"
          v-model.trim="password2"
          placeholder="请输入确认密码"
        />
        <input type="tel" v-model.trim="mobile" placeholder="请输入手机号" />
      </div>
      <div class="submit">
        <input type="submit" @click.prevent="register" value="注册" />
      </div>
    </form>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useUserStore } from '@/stores'
import { ElMessage } from 'element-plus'
import { registerApi } from '@/api/api'
const store = useUserStore()
const username = ref('')
const password = ref('')
const password2 = ref('')
const mobile = ref('')
const message = ref('')

async function register() {
  message.value = ''
  if (!checkForm()) return
  const { data } = await registerApi({
    username: username.value,
    password: password.value,
    mobile: mobile.value,
  })
  if (data) {
    store.saveUser({ username: data.username, id: data.id })
    username.value = ''
    password.value = ''
    password2.value = ''
    mobile.value = ''
  } else {
    message.value = '用户注册失败'
  }
}
// watch(username, (newval) => {
//   //取消上一次请求
//   if (newval) {
//     cancelRequest()
//     axios
//       .get('/user/' + newVal, {
//         cancelToken: new axios.CancelToken((cancel) => (cancel = cancel)),
//       })
//       .then((response) => {
//         if (response.data.code == 200) {
//           let isExist = response.data.data
//           if (isExist) {
//             message.value = '该用户名已经存在'
//           } else {
//             message.value = ''
//           }
//         }
//       })
//       .catch((error) => {
//         if (axios.isCancel(error)) {
//           //如果是请求被取消产生的错误，输出取消请求的原因
//           console.log('请求取消：', error.message)
//           //alert(error.message);
//           //throw new Error("请求取消：" + error.message)
//         } else {
//           //处理错误
//           console.log(error)
//           //throw new Error(error.message)
//         }
//       })
//   }
// })

// function cancelRequest() {
//   if (typeof cancel === 'function') {
//     cancel('终止请求')
//   }
// }
function checkForm() {
  if (!username.value || !password.value || !password2.value || !mobile.value) {
    ElMessage({
      message: '所有字段不能为空',
      type: 'error',
    })
    return false
  }
  if (password.value !== password2.value) {
    ElMessage({
      message: '密码和确认密码必须相同',
      type: 'error',
    })
    return false
  }
  return true
}
</script>

<style scoped>
.register {
  margin: 5em auto 0;
  width: 44%;
}

.register input {
  padding: 15px;
  width: 94%;
  font-size: 1.1em;
  margin: 18px 0px;
  color: gray;
  float: left;
  cursor: pointer;
  font-family: 'HelveticaNeue', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  outline: none;
  font-weight: 600;
  margin-left: 3px;
  background: #eee;
  transition: all 0.3s ease-out;
  border: solid 1px #ccc;
}

.register input:hover {
  color: rgb(180, 86, 9);
  border-left: solid 6px #40a46f;
}

.register .submit {
  padding: 5px 4px;
  text-align: center;
}
.register input[type='submit'] {
  padding: 17px 17px;
  color: #fff;
  float: right;
  font-family: 'HelveticaNeue', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  background: #40a46f;
  border: solid 1px #40a46f;
  cursor: pointer;
  font-size: 18px;
  transition: all 0.5s ease-out;
  outline: none;
  width: 100%;
}

.register .submit input[type='submit']:hover {
  background: #07793d;
  border: solid 1px #07793d;
}
.register .error {
  color: red;
  font-weight: bold;
  font-size: 1.1em;
}
</style>
